﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_khachhangInfo
    {
        private int _id_kh;
        private string _makh;
        private string _tenkhachhang;
        private string _diachi;
        private string _dienthoai;
        private string _email;
        private DateTime _ngaysinh;


        public cs_erp_khachhangInfo()
        {
            this._id_kh = 0;
            this._makh = " ";
            this._tenkhachhang = " ";
            this._diachi = " ";
            this._dienthoai = " ";
            this._email = " ";
            this._ngaysinh = Convert.ToDateTime("01/01/9999");

        }
        public int id_kh
        {
            get
            {
                return this._id_kh;
            }
            set
            {
                this._id_kh = value;
            }
        }
        public string makh
        {
            get
            {
                return this._makh;
            }
            set
            {
                this._makh = value;
            }
        }
        public string tenkhachhang
        {
            get
            {
                return this._tenkhachhang;
            }
            set
            {
                this._tenkhachhang = value;
            }
        }
        public string diachi
        {
            get
            {
                return this._diachi;
            }
            set
            {
                this._diachi = value;
            }
        }
        public string dienthoai
        {
            get
            {
                return this._dienthoai;
            }
            set
            {
                this._dienthoai = value;
            }
        }
        public string email
        {
            get
            {
                return this._email;
            }
            set
            {
                this._email = value;
            }
        }
        public DateTime ngaysinh
        {
            get
            {
                return this._ngaysinh;
            }
            set
            {
                this._ngaysinh = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[8];
            paraName[0] = "@id_kh";
            paraName[1] = "@makh";
            paraName[2] = "@tenkhachhang";
            paraName[3] = "@diachi";
            paraName[4] = "@dienthoai";
            paraName[5] = "@email";
            paraName[6] = "@ngaysinh";
            paraName[7] = "@option";
            object[] paraValue = new object[8];
            paraValue[0] = this._id_kh;
            paraValue[1] = this._makh;
            paraValue[2] = this._tenkhachhang;
            paraValue[3] = this._diachi;
            paraValue[4] = this._dienthoai;
            paraValue[5] = this._email;
            paraValue[6] = this._ngaysinh;
            paraValue[7] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 8, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_KHACHHANG", option);
        }

        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_KHACHHANG", "id_kh = " + this._id_kh);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_kh = Convert.ToInt32(ds.Tables[0].Rows[0]["id_kh"].ToString().Trim());
                this._makh = ds.Tables[0].Rows[0]["makh"].ToString().Trim();
                this._tenkhachhang = ds.Tables[0].Rows[0]["tenkhachhang"].ToString().Trim();
                this._diachi = ds.Tables[0].Rows[0]["diachi"].ToString().Trim();
                this._dienthoai = ds.Tables[0].Rows[0]["dienthoai"].ToString().Trim();
                this._email = ds.Tables[0].Rows[0]["email"].ToString().Trim();
                this._ngaysinh = Convert.ToDateTime(ds.Tables[0].Rows[0]["ngaysinh"].ToString().Trim());

                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public int Get_Info_by_MAKH()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_KHACHHANG", "makh = '" + this._makh + "'");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_kh = Convert.ToInt32(ds.Tables[0].Rows[0]["id_kh"].ToString().Trim());
                this._makh = ds.Tables[0].Rows[0]["makh"].ToString().Trim();
                this._tenkhachhang = ds.Tables[0].Rows[0]["tenkhachhang"].ToString().Trim();
                this._diachi = ds.Tables[0].Rows[0]["diachi"].ToString().Trim();
                this._dienthoai = ds.Tables[0].Rows[0]["dienthoai"].ToString().Trim();
                this._email = ds.Tables[0].Rows[0]["email"].ToString().Trim();
                this._ngaysinh = Convert.ToDateTime(ds.Tables[0].Rows[0]["ngaysinh"].ToString().Trim());

                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select id_kh, makh, tenkhachhang, diachi, dienthoai, email"
                + " from ERP_KHACHHANG"
                + " where id_kh <> 1";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_by_Condition(string condition)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select id_kh, makh, tenkhachhang, diachi, dienthoai, email"
                + " from ERP_KHACHHANG"
                + " where id_kh <> 1 and " + condition;
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}